package cn.edu.jxau.controller;



import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.jxau.domain.post;
import cn.edu.jxau.service.postingService;
import cn.itcast.commons.CommonUtils;

public class postingServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private postingService postingService=new postingService();
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String username=(String) request.getSession().getAttribute("username");
		//判断是否登录
		if(username==null){
			response.getWriter().write("<script type='text/javascript'>");
			response.getWriter().write("alert('你还没登录不能发帖')");
			response.getWriter().write("</script>");
			response.sendRedirect("login.jsp");
			return;
		}
		String content = request.getParameter("editorValue");
		String title=request.getParameter("title");
		String type=request.getParameter("type");
		//创建post对象，封装数据
		post post=new post();
		String postId=CommonUtils.uuid();
		post.setPostId(postId);
		post.setTitle(title);
		post.setContent(content);
		post.setPageView(0);
		post.setPoster(username);
		post.setType(type);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		post.setTime(df.format(new Date()));
		post.setValuable("0");
		
		
		//插入发的帖子
		
		postingService.add(post);
		
//		response.getWriter().write("<script type='text/javascript'>");
//		response.getWriter().write("alert('发帖成功')");
//		response.getWriter().write("</script>");
		
		request.setAttribute("postId",postId);
		request.getRequestDispatcher("findReplyServlet").include(request, response);
	}
}
